/*
 * RenderingWindow.java
 *
 * Created on 26 de Junho de 2008, 16:51
 */

package br.ufpe.cin.if680.lightmirror.ui;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOError;
import java.io.IOException;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

import sun.rmi.server.InactiveGroupException;

import br.ufpe.cin.if680.lightmirror.core.Parametros;
import br.ufpe.cin.if680.lightmirror.core.Inicializador;
import br.ufpe.cin.if680.lightmirror.core.Ponto;
import br.ufpe.cin.if680.lightmirror.core.Triangulo;
import br.ufpe.cin.if680.lightmirror.core.Vetor;

/**
 *
 * @author  Lailson Bandeira
 */
public class RenderingWindow extends javax.swing.JFrame {

    private static final String SEPARADOR_PADRAO = "\\s*[, ]+\\s*";
	public static final String titulo = "LightMirror UI [%d, %d]";
    protected BufferedImage tela;
    
    /** Creates new form RenderingWindow */
    public RenderingWindow() {
    	initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlTela = new javax.swing.JPanel() {
            public void paint(Graphics g) {
                super.paint(g);
                if(tela != null) {
                    g.drawImage(tela, 0, 0, null);
                }
            }
        } ;
        pnlParametros = new javax.swing.JPanel();
        pnlObjeto = new javax.swing.JPanel();
        lblka = new javax.swing.JLabel();
        txtka = new javax.swing.JTextField();
        lblkd = new javax.swing.JLabel();
        txtkd = new javax.swing.JTextField();
        lblks = new javax.swing.JLabel();
        txtks = new javax.swing.JTextField();
        lblod = new javax.swing.JLabel();
        txtod = new javax.swing.JTextField();
        lbleta = new javax.swing.JLabel();
        txteta = new javax.swing.JTextField();
        pnlCena = new javax.swing.JPanel();
        lblia = new javax.swing.JLabel();
        txtia = new javax.swing.JTextField();
        lblil = new javax.swing.JLabel();
        txtil = new javax.swing.JTextField();
        lblpl = new javax.swing.JLabel();
        txtpl = new javax.swing.JTextField();
        pnlCamera = new javax.swing.JPanel();
        lblc = new javax.swing.JLabel();
        txtc = new javax.swing.JTextField();
        lbln = new javax.swing.JLabel();
        txtn = new javax.swing.JTextField();
        lblv = new javax.swing.JLabel();
        txtv = new javax.swing.JTextField();
        lbld = new javax.swing.JLabel();
        txtd = new javax.swing.JTextField();
        lblh = new javax.swing.JLabel();
        txth = new javax.swing.JTextField();
        lblModelo = new javax.swing.JLabel();
        txtModelo = new javax.swing.JTextField();
        btnRenderizar = new javax.swing.JButton();
        btnCarregar = new javax.swing.JButton();
        lblExtras = new javax.swing.JLabel();
        txtExtras = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LightMirror [800x600]");
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        pnlTela.setBackground(new java.awt.Color(0, 0, 0));
        pnlTela.setMaximumSize(new java.awt.Dimension(800, 600));
        pnlTela.setMinimumSize(new java.awt.Dimension(800, 600));

        javax.swing.GroupLayout pnlTelaLayout = new javax.swing.GroupLayout(pnlTela);
        pnlTela.setLayout(pnlTelaLayout);
        pnlTelaLayout.setHorizontalGroup(
            pnlTelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        pnlTelaLayout.setVerticalGroup(
            pnlTelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );

        pnlObjeto.setBorder(javax.swing.BorderFactory.createTitledBorder("Atributos do Objeto"));

        lblka.setText("ka:");

        lblkd.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblkd.setText("kd:");
        lblkd.setToolTipText("R, G, B");

        lblks.setText("ks:");

        lblod.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblod.setText("Od:");
        lblod.setToolTipText("R, G, B");

        lbleta.setText("<html>&eta;:");

        javax.swing.GroupLayout pnlObjetoLayout = new javax.swing.GroupLayout(pnlObjeto);
        pnlObjeto.setLayout(pnlObjetoLayout);
        pnlObjetoLayout.setHorizontalGroup(
            pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlObjetoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblkd)
                    .addComponent(lblks)
                    .addComponent(lblod)
                    .addComponent(lbleta)
                    .addComponent(lblka))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtka, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(txtks, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(txtkd, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(txteta, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(txtod, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlObjetoLayout.setVerticalGroup(
            pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlObjetoLayout.createSequentialGroup()
                .addGroup(pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblka)
                    .addComponent(txtka, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblkd)
                    .addComponent(txtkd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblks)
                    .addComponent(txtks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblod)
                    .addComponent(txtod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlObjetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbleta)
                    .addComponent(txteta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlCena.setBorder(javax.swing.BorderFactory.createTitledBorder("Atributos da Cena"));

        lblia.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblia.setText("Ia:");

        lblil.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblil.setText("IL:");

        lblpl.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblpl.setText("PL:");

        javax.swing.GroupLayout pnlCenaLayout = new javax.swing.GroupLayout(pnlCena);
        pnlCena.setLayout(pnlCenaLayout);
        pnlCenaLayout.setHorizontalGroup(
            pnlCenaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCenaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCenaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblpl)
                    .addComponent(lblil)
                    .addComponent(lblia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(pnlCenaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtpl, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                    .addComponent(txtil, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                    .addComponent(txtia, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlCenaLayout.setVerticalGroup(
            pnlCenaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCenaLayout.createSequentialGroup()
                .addGroup(pnlCenaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblia)
                    .addComponent(txtia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCenaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblil)
                    .addComponent(txtil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCenaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblpl)
                    .addComponent(txtpl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlCamera.setBorder(javax.swing.BorderFactory.createTitledBorder("Câmera"));

        lblc.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblc.setText("C:");
        lblc.setToolTipText("x, y, z");

        lbln.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbln.setText("N:");
        lbln.setToolTipText("x, y, z");

        lblv.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblv.setText("V:");
        lblv.setToolTipText("x, y, z");

        lbld.setText("d:");

        lblh.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblh.setText("h:");
        lblh.setToolTipText("hx, hy");

        javax.swing.GroupLayout pnlCameraLayout = new javax.swing.GroupLayout(pnlCamera);
        pnlCamera.setLayout(pnlCameraLayout);
        pnlCameraLayout.setHorizontalGroup(
            pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCameraLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbld)
                    .addComponent(lblh)
                    .addGroup(pnlCameraLayout.createSequentialGroup()
                        .addGroup(pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblc)
                            .addComponent(lbln)
                            .addComponent(lblv))
                        .addGap(14, 14, 14)
                        .addGroup(pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtc, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(txtn, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(txtd, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(txtv, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(txth, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))))
                .addContainerGap())
        );
        pnlCameraLayout.setVerticalGroup(
            pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCameraLayout.createSequentialGroup()
                .addGroup(pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblc)
                    .addComponent(txtc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbln)
                    .addComponent(txtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblv)
                    .addComponent(txtv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbld)
                    .addComponent(txtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCameraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblh)
                    .addComponent(txth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblModelo.setText("Modelo:");

        btnRenderizar.setText("Renderizar");
        btnRenderizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRenderizarActionPerformed(evt);
            }
        });

        btnCarregar.setText("Carregar");
        btnCarregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCarregarActionPerformed(evt);
            }
        });

        lblExtras.setText("Extras:");

        txtExtras.setText("padrao.conf");

        javax.swing.GroupLayout pnlParametrosLayout = new javax.swing.GroupLayout(pnlParametros);
        pnlParametros.setLayout(pnlParametrosLayout);
        pnlParametrosLayout.setHorizontalGroup(
            pnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlParametrosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblModelo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(pnlCena, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlObjeto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlCamera, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(pnlParametrosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblExtras)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtExtras, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlParametrosLayout.createSequentialGroup()
                .addContainerGap(46, Short.MAX_VALUE)
                .addComponent(btnCarregar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRenderizar)
                .addContainerGap())
        );
        pnlParametrosLayout.setVerticalGroup(
            pnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlParametrosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblModelo)
                    .addComponent(txtModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlObjeto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlCena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblExtras)
                    .addComponent(txtExtras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRenderizar)
                    .addComponent(btnCarregar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlTela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlParametros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlTela, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlParametros, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void btnRenderizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRenderizarActionPerformed
		Parametros p = new Parametros();
		if(!extrairParametros(p)) return;
		System.out.println("Parâmetros validados com sucesso.");
		
		if(!lerModelo(p)) return;
		System.out.println("Modelo lido com sucesso.");
		
		renderizar(p);
	}//GEN-LAST:event_btnRenderizarActionPerformed


	private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
	    this.setTitle(String.format(titulo, pnlTela.getWidth(), pnlTela.getHeight()));
	}//GEN-LAST:event_formComponentResized

	private void btnCarregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregarActionPerformed
		carregarConfiguracao();
	}//GEN-LAST:event_btnCarregarActionPerformed

	private boolean extrairParametros(Parametros p) {
		p.setArquivoModelo(txtModelo.getText());
    	StringTokenizer extras = new StringTokenizer(txtExtras.getText());
    	if(extras.countTokens() == 2){
    		extras.nextToken();
    		p.setNPontosLuz(Integer.parseInt(extras.nextToken()));  		
    	}
		try {
			p.setKa(Double.parseDouble(txtka.getText()));
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Coeficiente ambiental Ka inválido (deve estar entre 0 e 1).");
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txtkd.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			p.setKd(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Constante difusa Kd inválida (deve estar no formato R, G, B, entre 0 e 1).");
			e.printStackTrace();
			return false;
		}
		
		try {
			p.setKs(Double.parseDouble(txtks.getText()));
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Constante especular Ks inválida (deve estar entre 0 e 1).");
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txtod.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			p.setOd(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Cor do objeto Od inválida (deve estar no formato R, G, B, entre 0 e 1).");
			e.printStackTrace();
			return false;
		}
		
		try {
			p.setEta(Double.parseDouble(txteta.getText()));
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Constante de rugosidade \u03b7 inválida (deve ser maior que 0).");
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txtia.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			p.setIa(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Iluminação da cena Ia inválida (deve estar no formato R, G, B, entre 0 e 255).");
			e.printStackTrace();
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txtil.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			
			Vetor[] ils = new Vetor[p.getNPontosLuz()];
			for(int i = 0; i < p.getNPontosLuz(); i++){
				ils[i] = new Vetor(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
			}
			p.setIl(ils);
		}catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Cor da luz IL inválida (deve estar no formato R, G, B, entre 0 e 255).");
			e.printStackTrace();
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txtpl.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			
			Ponto[] pls = new Ponto[p.getNPontosLuz()];
			for(int i = 0; i < p.getNPontosLuz(); i++){
				pls[i] = new Ponto(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
			}
			p.setPl(pls);
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Vetor do ponto de luz PL inválido (deve estar no formato x, y, z, entre 0 e 1).");
			e.printStackTrace();
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txtc.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			p.setC(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Vetor de foco C inválido (deve estar no formato x, y, z).");
			e.printStackTrace();
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txtn.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			p.setN(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Vetor de mira N inválido (deve estar no formato x, y, z).");
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txtv.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			p.setV(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Vetor de orientação vertical V inválido (deve estar no formato x, y, z).");
			return false;
		}
		
		try {
			p.setD(Double.parseDouble(txtd.getText()));
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Distância do foco ao plano d inválida (deve ser maior que 0).");
			return false;
		}
		
		try {
			Scanner scanner = new Scanner(txth.getText());
			scanner.useDelimiter(SEPARADOR_PADRAO);
			scanner.useLocale(Locale.US);
			p.setHx(scanner.nextDouble());
			p.setHy(scanner.nextDouble());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Dimensão da tela h inválida (deve estar no formato x, y).");
			return false;
		}
		
		return true;
	}
	
	protected boolean lerModelo(Parametros p) {
		try {
			Scanner scanner = new Scanner(new File(p.getArquivoModelo()));
			scanner.useLocale(Locale.US);
			
			Ponto[] pontos = new Ponto[scanner.nextInt()];
			Triangulo[] triangulos = new Triangulo[scanner.nextInt()];
			
			for(int i = 0; i < pontos.length; i++) {
				pontos[i] = new Ponto(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
			}
			System.out.printf("%d pontos lidos com sucesso.\n", pontos.length);
			
			for(int i = 0; i < triangulos.length; i++) {
				Ponto p1 = pontos[scanner.nextInt() - 1];
				Ponto p2 = pontos[scanner.nextInt() - 1];
				Ponto p3 = pontos[scanner.nextInt() - 1];
				triangulos[i] = new Triangulo(p1, p2, p3);
			}
			System.out.printf("%d triangulos lidos com sucesso.\n", triangulos.length);
			
			p.setPontos(pontos);
			p.setTriangulos(triangulos);
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(this, "Arquivo de modelo não encontrado.");
			return false;
		} catch (NoSuchElementException e) {
			JOptionPane.showMessageDialog(this, "Formato do arquivo de modelo inválido.");
			return false;
		}
		return true;
	}
	
    protected void renderizar(Parametros p) {
        int largura = pnlTela.getWidth();
        int altura = pnlTela.getHeight();
    	
    	tela = new BufferedImage(largura, altura, BufferedImage.TYPE_INT_ARGB);
    	p.setLargura(largura);
    	p.setAltura(altura);
    	p.setGrafico(tela.getGraphics());
    	
    	StringTokenizer extras = new StringTokenizer(txtExtras.getText());
    	if(extras.countTokens() == 2){
    		extras.nextToken();
    		p.setNPontosLuz(Integer.parseInt(extras.nextToken()));  		
    	}
    	
    	Inicializador.init(p);
    	
    	System.out.println("Imagem renderizada com sucesso.");
        pnlTela.repaint();
    }
    
    protected void carregarConfiguracao() {
    	try {
    		StringTokenizer extras = new StringTokenizer(txtExtras.getText());
			Scanner scanner = new Scanner(new File(extras.nextToken()));
			
			txtModelo.setText(scanner.nextLine());
			scanner.nextLine();
			
			txtka.setText(scanner.nextLine());
			txtkd.setText(scanner.nextLine());
			txtks.setText(scanner.nextLine());
			txtod.setText(scanner.nextLine());
			txteta.setText(scanner.nextLine());
			scanner.nextLine();
			
			txtia.setText(scanner.nextLine());
			txtil.setText(scanner.nextLine());
			txtpl.setText(scanner.nextLine());
			scanner.nextLine();
			
			txtc.setText(scanner.nextLine());
			txtn.setText(scanner.nextLine());
			txtv.setText(scanner.nextLine());
			txtd.setText(scanner.nextLine());
			txth.setText(scanner.nextLine() + " " + scanner.nextLine());
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(this, "Arquivo de configuração não encontrado.");
		} catch (NoSuchElementException e) {
			JOptionPane.showMessageDialog(this, "Formato do arquivo de configuração inválido.");
		}
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCarregar;
    private javax.swing.JButton btnRenderizar;
    private javax.swing.JLabel lblExtras;
    private javax.swing.JLabel lblModelo;
    private javax.swing.JLabel lblc;
    private javax.swing.JLabel lbld;
    private javax.swing.JLabel lbleta;
    private javax.swing.JLabel lblh;
    private javax.swing.JLabel lblia;
    private javax.swing.JLabel lblil;
    private javax.swing.JLabel lblka;
    private javax.swing.JLabel lblkd;
    private javax.swing.JLabel lblks;
    private javax.swing.JLabel lbln;
    private javax.swing.JLabel lblod;
    private javax.swing.JLabel lblpl;
    private javax.swing.JLabel lblv;
    private javax.swing.JPanel pnlCamera;
    private javax.swing.JPanel pnlCena;
    private javax.swing.JPanel pnlObjeto;
    private javax.swing.JPanel pnlParametros;
    private javax.swing.JPanel pnlTela;
    private javax.swing.JTextField txtExtras;
    private javax.swing.JTextField txtModelo;
    private javax.swing.JTextField txtc;
    private javax.swing.JTextField txtd;
    private javax.swing.JTextField txteta;
    private javax.swing.JTextField txth;
    private javax.swing.JTextField txtia;
    private javax.swing.JTextField txtil;
    private javax.swing.JTextField txtka;
    private javax.swing.JTextField txtkd;
    private javax.swing.JTextField txtks;
    private javax.swing.JTextField txtn;
    private javax.swing.JTextField txtod;
    private javax.swing.JTextField txtpl;
    private javax.swing.JTextField txtv;
    // End of variables declaration//GEN-END:variables

}
